package com.industrial.white.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.PageUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.domain.SysRole;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import com.industrial.white.domain.EmContingencyManagement;
import com.industrial.system.api.domain.EmEnterpriseInformationCollection;
import com.industrial.white.domain.WhiteIndustrialArea;
import com.industrial.white.mapper.EmContingencyManagementMapper;
import com.industrial.white.mapper.EmEnterpriseInformationCollectionMapper;
import com.industrial.white.mapper.WhiteIndustrialAreaMapper;
import com.industrial.white.service.IEmContingencyManagementService;
import com.industrial.white.vo.EmContingencyManagementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * 应急管理Service业务层处理
 * 
 * @author ZHOU_S
 * @date 2023-08-16
 */
@Service
public class EmContingencyManagementServiceImpl implements IEmContingencyManagementService
{
    @Autowired
    private EmContingencyManagementMapper emContingencyManagementMapper;
   @Autowired
    private EmEnterpriseInformationCollectionMapper emEnterpriseInformationCollectionMapper;
    @Autowired
    private WhiteIndustrialAreaMapper whiteIndustrialAreaMapper;
    @Override
    public List<EmContingencyManagementVo> groupList(EmContingencyManagement emContingencyManagement) {

        List<EmContingencyManagementVo> list=new ArrayList<>();
        //日期搜索
        SysUser user= SecurityUtils.getLoginUser().getSysUser();
        String  role= SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleKey();
        if("p_01_011".contains(role)){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getProvince());
            emContingencyManagement.setProvince(whiteIndustrialArea.getBaseId());
            list=   emContingencyManagementMapper.groupProvinceList(emContingencyManagement);
            }else if("s_01_011".contains(role)){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getCity());
            emContingencyManagement.setCity(whiteIndustrialArea.getBaseId());
            list=   emContingencyManagementMapper.groupCityList(emContingencyManagement);
        }else if("x_01_011".contains(role)){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getDistrict());
            emContingencyManagement.setCounty(whiteIndustrialArea.getBaseId());
            list=emContingencyManagementMapper.groupCountyList(emContingencyManagement);
        }else if("q_01_001".contains(role)||"white".contains(role)){

            EmEnterpriseInformationCollection emEnterpriseInformationCollection=
                    emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
            if(emEnterpriseInformationCollection!=null){
                emContingencyManagement.setEnterpriseId(emEnterpriseInformationCollection.getBaseId());
            }
            list=emContingencyManagementMapper.groupEnterpriseList(emContingencyManagement);
        }
        return list;
    }

    /**
     * 查询应急管理
     * 
     * @param baseId 应急管理主键
     * @return 应急管理
     */
    @Override
    public EmContingencyManagement selectEmContingencyManagementByBaseId(String baseId)
    {
        return emContingencyManagementMapper.selectEmContingencyManagementByBaseId(baseId);
    }

    /**
     * 查询应急管理列表
     * 
     * @param emContingencyManagement 应急管理
     * @return 应急管理
     */
    @Override
    public List<EmContingencyManagement> selectEmContingencyManagementList(EmContingencyManagement emContingencyManagement)
    {
        List<EmContingencyManagement> list=new ArrayList<>();
        //日期搜索
        SysUser user= SecurityUtils.getLoginUser().getSysUser();
//        String  role= SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleKey();
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(user.isAdmin()){
            roles.add("p_01_011");
        }

        if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getProvince());
            emContingencyManagement.setProvince(whiteIndustrialArea.getBaseId());
        }else if(ArrayUtil.contains(roles.toArray(),"s_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getCity());
            emContingencyManagement.setCity(whiteIndustrialArea.getBaseId());
        }else if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getDistrict());
            emContingencyManagement.setCounty(whiteIndustrialArea.getBaseId());
        }else if(ArrayUtil.contains(roles.toArray(),"q_01_001")){
            EmEnterpriseInformationCollection emEnterpriseInformationCollection=
                    emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
            if(emEnterpriseInformationCollection!=null){
                emContingencyManagement.setEnterpriseId(emEnterpriseInformationCollection.getBaseId());
            }
        }else if(ArrayUtil.contains(roles.toArray(),"white")){
            EmEnterpriseInformationCollection emEnterpriseInformationCollection=
                    emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
            emContingencyManagement.setEnterpriseId(emEnterpriseInformationCollection.getBaseId());
        }

//        if("p_01_011".contains(role)){
//            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getProvince());
//            emContingencyManagement.setProvince(whiteIndustrialArea.getBaseId());
//        }else if("s_01_011".contains(role)){
//            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getCity());
//            emContingencyManagement.setCity(whiteIndustrialArea.getBaseId());
//        }else if("x_01_011".contains(role)){
//            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getDistrict());
//            emContingencyManagement.setCounty(whiteIndustrialArea.getBaseId());
//        }else if("q_01_001".contains(role)){
//            EmEnterpriseInformationCollection emEnterpriseInformationCollection=
//                    emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
//            if(emEnterpriseInformationCollection!=null){
//                emContingencyManagement.setEnterpriseId(emEnterpriseInformationCollection.getBaseId());
//            }
//        }else if("white".contains(role)){
//            EmEnterpriseInformationCollection emEnterpriseInformationCollection=
//                    emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
//            emContingencyManagement.setEnterpriseId(emEnterpriseInformationCollection.getBaseId());
//        }
        PageUtils.startPage();
        list=  emContingencyManagementMapper.selectEmContingencyManagementList(emContingencyManagement);
        return list;
    }

    /**
     * 新增应急管理
     * 
     * @param emContingencyManagement 应急管理
     * @return 结果
     */
    @Override
    public int insertEmContingencyManagement(EmContingencyManagement emContingencyManagement)
    {
        SysUser user= SecurityUtils.getLoginUser().getSysUser();
        EmEnterpriseInformationCollection emEnterpriseInformationCollection=
                emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());


        EmContingencyManagement em=new EmContingencyManagement();
        em.setEnterpriseId(emEnterpriseInformationCollection.getBaseId());
        em.setStartTime(DateUtils.getDate());
/*        em.setEndTime(DateUtils.getDate());*/
        List<EmContingencyManagement> list=emContingencyManagementMapper.selectEmContingencyManagementList(em);
        int flag=0;
        if(list.size()>0){
        }else{
            String id= UUID.randomUUID().toString().replaceAll("-","");
            emContingencyManagement.setBaseId(id);
            emContingencyManagement.setEnterpriseId(emEnterpriseInformationCollection.getBaseId());
            emContingencyManagement.setBaseCreateTime(DateUtils.getNowDate());
            emContingencyManagement.setBaseUpdateTinme(DateUtils.getNowDate());
            flag=  emContingencyManagementMapper.insertEmContingencyManagement(emContingencyManagement);
        }

        return flag;
    }

    /**
     * 修改应急管理
     * 
     * @param emContingencyManagement 应急管理
     * @return 结果
     */
    @Override
    public int updateEmContingencyManagement(EmContingencyManagement emContingencyManagement)
    {
        return emContingencyManagementMapper.updateEmContingencyManagement(emContingencyManagement);
    }

    /**
     * 批量删除应急管理
     * 
     * @param baseIds 需要删除的应急管理主键
     * @return 结果
     */
    @Override
    public int deleteEmContingencyManagementByBaseIds(String[] baseIds)
    {
        return emContingencyManagementMapper.deleteEmContingencyManagementByBaseIds(baseIds);
    }

    /**
     * 删除应急管理信息
     * 
     * @param baseId 应急管理主键
     * @return 结果
     */
    @Override
    public int deleteEmContingencyManagementByBaseId(String baseId)
    {
        return emContingencyManagementMapper.deleteEmContingencyManagementByBaseId(baseId);
    }

    @Override
    public boolean updateByBaseIds(EmContingencyManagement[] emContingencyManagements) {

        int updates=0;
        boolean flag=false;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        for (int i = 0; i < emContingencyManagements.length; i++) {
            updates+=1;
            if(roles.contains("p_01_011")){
                emContingencyManagements[i].setProvinceStatus("1");
            }else if(roles.contains("s_01_011")){
                emContingencyManagements[i].setProvinceStatus("2");
            }else if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                emContingencyManagements[i].setProvinceStatus("3");
            }

            emContingencyManagementMapper.updateEmContingencyManagement(emContingencyManagements[i]);
        }
        if(updates==emContingencyManagements.length){
            flag=true;
        }
        return  flag;
    }

}
